// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <QApplication>
#include <iostream>

#include "OpenMeca/Gui/WidgetDouble.hpp"
#include "OpenMeca/Util/Dimension.hpp"
#include "OpenMeca/Util/Unit.hpp"

namespace OpenMeca
{
  namespace Gui
  {
    WidgetDouble::WidgetDouble(QWidget* parent)
      :QWidget(parent),
       WidgetInterface<double>(*parent),
       dim_(0)
    {
      Ui::WidgetDouble::setupUi(this);
    }

    WidgetDouble::~WidgetDouble()
    {
    }

    
    void 
    WidgetDouble::SetDimension(const Util::Dimension& dim)
    {
      dim_ = &dim;
      unitLabel_->setText(dim_->GetUserChoice().GetSymbol().c_str());
    }

     void 
     WidgetDouble::Init()
    {
      const double value =  WidgetInterface<double>::GetValue()/dim_->GetUserChoice().GetFactor();
      lineEdit_->setText(QString::number(value));
    }
    

    void 
    WidgetDouble::SetLabel(const QString& str)
    {
      label_->setText(str);
    }
   
    bool
    WidgetDouble::Check()
    {
      bool conversionOk = false;
      const double val = lineEdit_->text().toDouble(&conversionOk);
      if (conversionOk==false)
	{
	  QPalette p = lineEdit_->palette();
	  p.setColor(QPalette::Base, Qt::red);
	  lineEdit_->setPalette(p);
	}
      else
	{
	  lineEdit_->setPalette(QApplication::palette());
	  WidgetInterface<double>::GetCopy() = val*dim_->GetUserChoice().GetFactor();
	}
      return conversionOk;
    }

    QLineEdit&
    WidgetDouble::GetLineEdit()
    {
      return *lineEdit_;
    }

  }
}


